package bb.lanxing.mvp.presetner;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import com.alibaba.fastjson.parser.deserializer.ParseProcess;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.common.cache.CacheManager;
import bb.lanxing.common.cache.CacheType;
import bb.lanxing.model.database.User;
import bb.lanxing.model.json.club.ClubNews;
import bb.lanxing.model.json.club.ClubNewsDeserializer;
import bb.lanxing.model.json.club.NewsText;
import bb.lanxing.util.Log;
import gov.nist.core.Separators;

public class ClubPresenter {
    private static ClubPresenter INSTANCE = null;

    private static List<WeakReference<IClubEventListener>> eventListener;
    private static Handler mEventHandler;

    public static final Gson GSON =
            new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").registerTypeAdapter(ClubNews.class, new ClubNewsDeserializer()).create();

    public interface IClubEventListener {
        boolean onClubEvent(int i, long j, Object obj);
    }

    public static ClubPresenter getInstance() {
        synchronized (ClubPresenter.class) {
            if (INSTANCE == null) {
                INSTANCE = new ClubPresenter();
            }
        }
        return INSTANCE;
    }

    public void clear() {
        CacheManager.getInstance().delete(CacheType.Club);
    }

    private ClubPresenter() {
    }

    public ClubNews parseNews(String str) {
        return (ClubNews) JSON.parseObject(str, ClubNews.class, (ParseProcess) (ExtraProcessor) (obj, str2, obj2) -> {
            if ("content_text".equals(str2)) {
                ((ClubNews) obj).setContent(new NewsText(obj2.toString()));
            }
        }, new Feature[0]);
    }

    public static ClubPresenter create() {
        return getInstance();
    }

    public static long getPrimaryClubId() {
        User signinUser;
        if (!App.getContext().isUserSignin() || (signinUser = App.getContext().getSigninUser()) == null) {
            return -1L;
        }
        String mainTeam = signinUser.getMainTeam();
        if (TextUtils.isEmpty(mainTeam) || !mainTeam.matches("(([^,^:]+:\\d+)|(,[^:^,]+:\\d+))+")) {
            return -1L;
        }
        return Long.parseLong(mainTeam.split(",")[0].split(Separators.COLON)[1]);
    }

    public static synchronized void registerClubEventListener(IClubEventListener iClubEventListener) {
        synchronized (ClubPresenter.class) {
        }
    }

    public static synchronized void unregisterClubEventListener(IClubEventListener iClubEventListener) {
        synchronized (ClubPresenter.class) {
            if (iClubEventListener == null) {
                return;
            }
            List<WeakReference<IClubEventListener>> list = eventListener;
            if (list != null && !list.isEmpty()) {
                Iterator<WeakReference<IClubEventListener>> it = eventListener.iterator();
                while (it.hasNext()) {
                    IClubEventListener iClubEventListener2 = it.next().get();
                    if (iClubEventListener2 != null && iClubEventListener2.equals(iClubEventListener)) {
                        it.remove();
                    }
                }
            }
        }
    }

    public static void postClubEvent(int i, long j, Object obj) {
        if (eventListener != null) {
            if (mEventHandler == null) {
                mEventHandler = new EventHandler(Looper.getMainLooper());
            }
            for (int i2 = 0; i2 < eventListener.size(); i2++) {
                mEventHandler.sendMessage(mEventHandler.obtainMessage(0, i2, i, new Pair<>(j, obj)));
            }
        }
    }

    public static void release() {
        Handler handler = mEventHandler;
        if (handler != null) {
            handler.removeMessages(0);
            mEventHandler = null;
        }
        List<WeakReference<IClubEventListener>> list = eventListener;
        if (list != null) {
            list.clear();
            eventListener = null;
        }
    }

    public static class EventHandler extends Handler {
        public EventHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message message) {
            int i = message.arg1;
            if (ClubPresenter.eventListener == null || i >= ClubPresenter.eventListener.size()) {
                return;
            }
            WeakReference<IClubEventListener> weakReference = ClubPresenter.eventListener.get(i);
            IClubEventListener iClubEventListener = weakReference != null ? (IClubEventListener) weakReference.get() : null;
            Pair pair = (Pair) message.obj;
            if (iClubEventListener != null) {
                try {
                    iClubEventListener.onClubEvent(message.arg2, (Long) pair.first, pair.second);
                } catch (Exception e) {
                    Log.e("club", "", e);
                }
            }
        }
    }
}